//
//  ViewController.m
//  TestProject
//
//  Created by 李萍 on 2017/4/13.
//  Copyright © 2017年 李萍. All rights reserved.
//

#import "ViewController.h"
#import "CountButton.h"
#import "LPStarView.h"

#import "UIColor+CustomColor.h"

#import <pop/pop.h>

@interface ViewController () <countButtonDelegate>
{
    CountButton *newButton;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    newButton = [[CountButton alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    newButton.layer.borderWidth = 1;
    newButton.layer.borderColor = [UIColor themeColor].CGColor;
    newButton.clipsToBounds = YES;
    newButton.layer.cornerRadius = 5;
    newButton.delegate = self;
    [self.view addSubview:newButton];
    
    
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)]];
    self.view.userInteractionEnabled = YES;
    
    
    
    UIButton *button = [UIButton new];
    [button setTitle:@"点击" forState:UIControlStateNormal];
    button.frame = CGRectMake(20, 200, 50, 50);
    button.backgroundColor = [UIColor themeColor];
    [self.view addSubview:button];
//    [button addTarget:self action:@selector(popAnimation) forControlEvents:UIControlEventTouchUpInside];
    
//    POPBasicAnimation *anBasic = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionX];
//    anBasic.toValue = @(button.center.y+100);
//    anBasic.beginTime = CACurrentMediaTime() + 1.0f;
//    [button pop_addAnimation:anBasic forKey:@"position"];
    
    POPSpringAnimation *anSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    anSpring.toValue = @(button.center.y+100);
    anSpring.beginTime = CACurrentMediaTime() + 1.0f;
    anSpring.springBounciness = 20.0f;
    [button pop_addAnimation:anSpring forKey:@"position"];
    

    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, 100, 30)];
    timeLabel.text = @"00:00:00";
    timeLabel.textColor = [UIColor magentaColor];
    timeLabel.backgroundColor = [UIColor redColor];
    [self.view addSubview:timeLabel];
    
    POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:@"countdown" initializer:^(POPMutableAnimatableProperty *prop) {
        prop.writeBlock = ^(id obj, const CGFloat values[]) {
            UILabel *lable = (UILabel*)obj;
            timeLabel.text = [NSString stringWithFormat:@"d:d:d",(int)values[0]/60,(int)values[0]%60,(int)(values[0]*100)];
        };
        //        prop.threshold = 0.01f;
    }];
    POPBasicAnimation *anBasic = [POPBasicAnimation linearAnimation];   //秒表当然必须是线性的时间函数
    anBasic.property = prop;    //自定义属性
    anBasic.fromValue = @(0);   //从0开始
    anBasic.toValue = @(3*60);  //180秒
    anBasic.duration = 3*60;    //持续3分钟
    anBasic.beginTime = CACurrentMediaTime() + 1.0f;    //延迟1秒开始
    [timeLabel pop_addAnimation:anBasic forKey:@"countdown"];
    
    //评论星星图案
    LPStarView *tggStarEvaView1 = [LPStarView starViewWithChooseStarBlock:^(NSUInteger count) {
        NSLog(@"几星评论：%lu", (unsigned long)count);
    }];
    tggStarEvaView1.frame = CGRectMake(20, 450, 300, 30);
    [self.view addSubview:tggStarEvaView1];
    
//    tggStarEvaView1.tapEnabled = NO;
//    tggStarEvaView1.starCount = 4;
    tggStarEvaView1.starSpacing = CGRectGetWidth(tggStarEvaView1.frame) / 20;
    tggStarEvaView1.starWidth = tggStarEvaView1.starSpacing*2;
    
    
}

- (void)tapClick
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - pop animation
- (void)popAnimation
{
    NSLog(@"pop animation");
    
}
    
#pragma mark - countButtonDelegate
- (void)countButtonCountChange:(CountButtonType)buttonType
{
    switch (buttonType) {
        case CountButtonTypeForSub:
        {
            if (newButton.countNum >= 1) {
                newButton.countNum--;
            } else { }
            
            break;
        }
        case CountButtonTypeForAdd:
        {
            newButton.countNum++;
            break;
        }
        default:
        break;
    }
}
    

@end
